﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Alone;
using Alone.GameObjects;
using AloneLibrary.GameObjects;
using AloneLibrary.Services.Camera;
using Alone.Handlers;

namespace Alone.GameObjects
{
    public class Weapon : DrawableGameComponent
    {
        private Model model;
        private Vector3 offset;
        private BasePlayer player;
        private Vector3 position;
        private int ammo;
        private float scale;
        private Matrix world;
        private ICameraHandler camera;
        private float rotation;

        public Weapon(Game game, Model model, BasePlayer player, Vector3 offset, float scale, ICameraHandler camera)
            : base(game)
        {
            this.player = player;
            this.offset = Vector3.Zero;
            //this.offset = new Vector3(0, 50, 0);
            this.model = model;
            this.scale = scale;
            this.camera = camera;
            this.ammo = 20;

        }

        public int Ammo
        {
            get { return ammo; }
            set { ammo = value; }
        }

        public void Update()
        {
            this.position = Vector3.Add(player.Position, offset);
        }

        public override void Draw(GameTime gameTime)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            world = Matrix.CreateScale(scale)
               * Matrix.CreateTranslation(position);

            foreach (ModelMesh modmesh in model.Meshes)
            {
                foreach (BasicEffect effect in modmesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    //effect.World = world * transforms[modmesh.ParentBone.Index];
                    effect.World = world;
                    effect.Projection = camera.Projection;
                    effect.View = camera.View;
                }
                modmesh.Draw();
            }
            base.Draw(gameTime);
        }
               
    }
}
